package cz.vutbr.fit.dnacompress.alignment;

import java.io.FileInputStream;
import java.util.HashMap;

import org.biojava.bio.program.ssbind.SeqSimilarityAdapter;
import org.biojava.bio.search.SearchContentHandler;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;

import cz.vutbr.fit.dnacompress.Main;

/**
 * zarovnani pomoci FASTA
 */
public class FastaAlignmentMaker extends AbstractAlignmentMaker {
	
	@Override
	public void init( String referenceFileName, String queryFileName) {	
		
		// jmeno souboru s vystupem z fasty
		outputFileName = Main.basename( queryFileName) + "-fasta.out";
		
		// nazev program pro zarovnani
		program = "fasta35";
		
		// parametry pro zarovnavaci program
		command = "-d 1 -m 10 -Q -O "+ outputFileName + 
        		" " + queryFileName + " " + referenceFileName;
	}
	
	@Override
	public void parse() {
		
		// parse fasta alignemnt output and make compressed file
		try{
			// parse fasta aligment
			InputSource is = new InputSource(new FileInputStream( Main.dataFolder + outputFileName)); 
			//make a FastaSearchSAXParser 
		    FastaSearchSAXParser parser = new FastaSearchSAXParser(); 
		    ContentHandler handler = new SeqSimilarityAdapter();
		    
		    //use our custom SearchContentHandler (see below)
		    HashMap<String, String> sequenceMap = new HashMap<String, String>();
		    SearchContentHandler scHandler = new FastaSearchContentHandler( sequenceMap);
		    ((SeqSimilarityAdapter)handler).setSearchContentHandler(scHandler);
		    parser.setContentHandler(handler); 
		    parser.parse(is);
		    
		    query = sequenceMap.get("querySequence");
		    reference = sequenceMap.get("subjectSequence");
		    
		}catch (Exception e){//Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}

	}
}
